package itsm.isperp.workflow.utils;

import groovy.lang.Binding;
import groovy.lang.GroovyShell;
import itsm.isperp.framework.core.context.ContextHolder;
import itsm.isperp.framework.domain.IUser;
import itsm.isperp.framework.message.MessageQueue;
import itsm.isperp.framework.message.domain.IMessagePacket;
import itsm.isperp.framework.template.Template;
import itsm.isperp.framework.template.TemplateException;
import itsm.isperp.module.entity.bpm.BpmNodeSetting;
import itsm.isperp.module.entity.db.DbEntity;
import itsm.isperp.module.service.bpm.BpmNodeSettingService;
import itsm.isperp.module.service.bpm.BpmProcessSettingService;
import itsm.isperp.module.service.db.DbEntityService;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.activiti.engine.delegate.VariableScope;
import org.apache.commons.lang3.StringUtils;
import org.springframework.jdbc.core.JdbcTemplate;

/**
 * 
 * BPM消息工具
 * 
 * @author lizx
 * @date 2013-12-7
 * @since 1.0
 * 
 */
public class BpmMessageUtils {
	/**
	 * 添加信息到队列中
	 * 
	 * @param mapUsers
	 *            接受对象列表
	 * @param ms
	 *            消息队列
	 * @param title
	 *            标题
	 * @param content
	 *            内容
	 * @param messageType
	 *            类别
	 * @throws TemplateException
	 */
	public static final void sendMessages(final VariableScope ent,
			String processDefKey, String nodeId,
			final Map<String, IUser> mapUsers, final String businessKey) {

		BpmNodeSettingService bpmNodeSettingService = ContextHolder
				.getSpringBean("bpmNodeSettingService");
		// 发送消息
		// 1、获取消息标题和内容(获取节点开始的标题和内容)
		BpmNodeSetting bNode = bpmNodeSettingService.findByNodeId(nodeId);

		String content = bNode.getStartMessage();

		if (StringUtils.isEmpty(bNode.getMessageType())
				|| StringUtils.isEmpty(content)) {

			return;
		}
		String title = bNode.getStartMessageTitle();

		Map<String, Object> context = new HashMap<>();

		// 加入流程定义的变量
		context.putAll(ent.getVariables());

		Template template = new Template();

		Object model = ent.getVariable("bpmBusinessModel");
		if (model == null) {

			DbEntityService dbEntityService = ContextHolder
					.getSpringBean("dbEntityService");

			BpmProcessSettingService bpmProcessSettingService = ContextHolder
					.getSpringBean("bpmProcessSettingService");

			DbEntity entity = bpmProcessSettingService
					.findDbEntityByKey(processDefKey);
			try {
				Map<String, Object> entityMap = dbEntityService
						.getDataFromDbEntity(entity, businessKey);

				// 加入流程实体的
				// TODO 根据actDefId获取到流程关联实体，再根据businessKey获取到具体胡数据
				context.put("bpmBusinessModel", entityMap);
			} catch (SQLException e) {

			}
		}

		// 加入当前登陆者
		context.put("bpmCurrentUser", ent.getVariable("bpmCurrentUser"));

		String script = bNode.getStartMessageScript();

		if (StringUtils.isNotEmpty(script)) {
			JdbcTemplate jdbcTemplate = ContextHolder
					.getSpringBean("jdbcTemplate");
			context.put("dao", jdbcTemplate);
			Binding bd = new Binding(context);
			GroovyShell shell = new GroovyShell(bd);
			Object obj = shell.evaluate(script);

			if (obj instanceof Map) {
				context.putAll((Map) obj);
			}
		}

		template.setContext(context);

		// 消息队列
		List<IMessagePacket> ms = new ArrayList<>();

		MessageQueue.generalAndAllToMeessageList(businessKey,
				bNode.getMessageType(), mapUsers, title, content, ms, template);
		// 发送消息队列
		MessageQueue.addToMessageQuery(ms);
	}
}
